package br.ufrj.neg.reuniao.servlet;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.bean.Idioma;
import br.ufrj.neg.constants.General;
import br.ufrj.neg.reuniao.bean.ArtefatoBean;
import br.ufrj.neg.reuniao.bean.ReuniaoBean;
import br.ufrj.neg.reuniao.bean.TipoArtefatoBean;
import br.ufrj.neg.reuniao.bean.TipoReuniaoBean;
import br.ufrj.neg.reuniao.model.TipoArtefatoModel;
import br.ufrj.neg.reuniao.model.TipoReuniaoModel;
import br.ufrj.neg.servlet.PageContext;
import br.ufrj.neg.util.Data;

public class CadastrarReuniaoAction extends VelocityViewServlet {

	private static final long serialVersionUID = -1502095409248120929L;

	// private static Logger log = Logger.getLogger("rootLogger");

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		
		String idNegocio = (String) (request.getParameter("idNegocio") );   		   		
		String novaEdicao = request.getParameter("novaEdicao");
		
		HttpSession session = request.getSession(false);
		
		ReuniaoBean rb = new ReuniaoBean();
		rb.setIdNegociacao(Integer.valueOf(idNegocio));
		
		synchronized(session){
			session.removeAttribute("NegociacaoBean");
			
			if( novaEdicao!=null ){
				session.setAttribute("ReuniaoBean",rb);
			}			
						
		}		
		
		carregarReuniao( Integer.valueOf(idNegocio) , null, request, response);			
				
	}	

	private void carregarReuniao(int idNegocio,ArtefatoBean relatorio,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		Idioma idioma = (Idioma) request.getAttribute("idioma");
		TipoArtefatoModel tam = new TipoArtefatoModel();
		TipoReuniaoModel trm = new TipoReuniaoModel();
		TipoReuniaoBean trb = new TipoReuniaoBean(idioma);
		ReuniaoBean rb = new ReuniaoBean();		
		
		HttpSession session = request.getSession();
		
		synchronized(session){
			rb = (ReuniaoBean) session.getAttribute("ReuniaoBean");
		}
		
		if( relatorio != null){
			relatorio.setIndice(rb.getArtefatos().size());
			rb.getArtefatos().add(relatorio);	
		}
		
		try {						
			setContentType(request, response);
			
			Context context = createContext(request, response);
			fillContext(context, request);
			
			context.put("pageContext", new PageContext(request));
			context.put("tipoReuniaoDataSet", trm.listaTipoReuniao(trb));
			TipoArtefatoBean tab = new TipoArtefatoBean(idioma);
			context.put("tipoArtefatoDataSet", tam.listaTipoArtefato(tab));
			context.put("reuniao", rb);
			context.put("addArtefatoFunction",!request.getHeader("user-agent").contains("MSIE"));
	
			
			Template template = getTemplate(General.TEMPLATE_REUNIAO_PATH + "cadastraReuniao.vm");
			mergeTemplate(template, context, response);
			
		} catch (Exception e) {
			System.out.println(e);
			request.getRequestDispatcher(General.TELA_ERRO_500).forward(request,
					response);
		}			
	}

}
